package CCF_2014;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class T201409_3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String paString = ".*"+scanner.nextLine()+".*";
        int flag = Integer.valueOf(scanner.nextLine())==0?Pattern.CASE_INSENSITIVE:0;
        Pattern pattern = Pattern.compile(paString, flag);
        int count = Integer.valueOf(scanner.nextLine());
        for (int i = 0; i < count; i++) {
            String str = scanner.nextLine();
            Matcher matcher = pattern.matcher(str);
            if (matcher.matches()) {
                System.out.println(str);
            }
        }
        scanner.close();
    }

}
